package Formularios;

import java.util.List;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.naming.spi.DirStateFactory.Result;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Rebecca
 */
public class viewCadastroAtend extends javax.swing.JPanel {

    /** Creates new form viewCadastroAtend */
    public viewCadastroAtend() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        label1 = new java.awt.Label();
        jComboBoxFuncionarios = new javax.swing.JComboBox();
        jComboBoxHorarios = new javax.swing.JComboBox();
        jComboBoxClientes = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaObs = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jCalendar1 = new com.toedter.calendar.JCalendar();
        jTextFieldValor = new javax.swing.JFormattedTextField();
        jTextFieldDuracao = new javax.swing.JFormattedTextField();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("jButton1");

        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        label1.setAlignment(java.awt.Label.CENTER);
        label1.setFont(new java.awt.Font("Dialog", 1, 18));
        label1.setText("Cadastro do Atendimento");

        jComboBoxFuncionarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
               // jComboBoxFuncionariosMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
               // jComboBoxFuncionariosMouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
               // jComboBoxFuncionariosMousePressed(evt);
            }
        });
        jComboBoxFuncionarios.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxFuncionariosPopupMenuWillBecomeVisible(evt);
            }
        });
        jComboBoxFuncionarios.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                //jComboBoxFuncionariosMouseDragged(evt);
            }
        });
        jComboBoxFuncionarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxFuncionariosActionPerformed(evt);
            }
        });
        jComboBoxFuncionarios.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
               // jComboBoxFuncionariosFocusGained(evt);
            }
        });
        jComboBoxFuncionarios.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
               // jComboBoxFuncionariosKeyPressed(evt);
            }
        });

        jComboBoxHorarios.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
             //   jComboBoxHorariosPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxHorariosPopupMenuWillBecomeVisible(evt);
            }
        });

        jComboBoxClientes.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxClientesPopupMenuWillBecomeVisible(evt);
            }
        });

        jLabel1.setText("Funcionário:");

        jLabel2.setText("Horário: ");

        jLabel3.setText("Cliente:");

        jLabel4.setText("Valor:");

        jLabel5.setText("Duração: ");

        jLabel6.setText("Observação:");

        jLabel7.setText("minutos");

        jTextAreaObs.setColumns(20);
        jTextAreaObs.setRows(5);
        jScrollPane2.setViewportView(jTextAreaObs);

        jButton2.setText("Incluir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Limpar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jTextFieldValor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        jTextFieldDuracao.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(160, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton3)
                                .addGap(18, 18, 18)
                                .addComponent(jButton2))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(49, 49, 49)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(30, 30, 30)
                                        .addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(18, 18, 18)
                                        .addComponent(jTextFieldDuracao, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel7))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jComboBoxClientes, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jComboBoxHorarios, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jComboBoxFuncionarios, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 603, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(68, 68, 68))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(285, 285, 285))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jComboBoxFuncionarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jComboBoxHorarios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jComboBoxClientes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldDuracao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxFuncionariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxFuncionariosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxFuncionariosActionPerformed

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        jComboBoxClientes.removeAllItems();
        jComboBoxFuncionarios.removeAllItems();
        jComboBoxHorarios.removeAllItems();
        jTextFieldValor.setText("");
        jTextFieldDuracao.setText("");
        jTextAreaObs.setText("");


    }//GEN-LAST:event_formAncestorAdded

    private void jComboBoxFuncionariosPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxFuncionariosPopupMenuWillBecomeVisible
        // TODO add your handling code here:
        jComboBoxHorarios.removeAllItems();
        try {

            ResultSet rs = SalaoApp.controle.consultaFuncionario();

            DefaultComboBoxModel model = new DefaultComboBoxModel();

            jComboBoxFuncionarios.setModel(model);
            jComboBoxFuncionarios.addItem("");
            while (rs.next()) {
                String nomeTelefone = rs.getString(5) + " - " + rs.getString(2);
                jComboBoxFuncionarios.addItem(nomeTelefone);
            }
            rs.close();

        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(1);
        }


        //JOptionPane.showMessageDialog(null, jCalendar1.getCalendar().getTime());
    }//GEN-LAST:event_jComboBoxFuncionariosPopupMenuWillBecomeVisible

    private void jComboBoxHorariosPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxHorariosPopupMenuWillBecomeVisible
        // TODO add your handling code here:
        jComboBoxHorarios.removeAllItems();
        Calendar dia = jCalendar1.getCalendar();
        SimpleDateFormat formata = new SimpleDateFormat("yyyy-MM-dd");
        String data = formata.format(dia.getTime()).toString();


        String cpf = jComboBoxFuncionarios.getSelectedItem().toString();
        if (cpf.length() == 0) {
            JOptionPane.showMessageDialog(null, "Informe um funcionario!");
        } //criar uma função de controle para fazer um select, depois tratar o resultado do select para gerar a combobox
        else {
            cpf = cpf.toString().substring(0, 14);
            List<String> lista = null;
            lista = SalaoApp.controle.consultaHorario(data, cpf.toString());
            for (int i = 0; i < lista.size(); i++) {
                jComboBoxHorarios.addItem(lista.get(i));
            }
        }//else

    }//GEN-LAST:event_jComboBoxHorariosPopupMenuWillBecomeVisible

    private void jComboBoxClientesPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxClientesPopupMenuWillBecomeVisible
        // TODO add your handling code here:
        jComboBoxClientes.removeAllItems();
        try {
            ResultSet cliente = SalaoApp.controle.consultaCliente();
            while (cliente.next()) {
                jComboBoxClientes.addItem(cliente.getString(6).toString() + " " + cliente.getString(5).toString());
            }//while
        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(1);
        }
    }//GEN-LAST:event_jComboBoxClientesPopupMenuWillBecomeVisible

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        Calendar dia = jCalendar1.getCalendar();
        SimpleDateFormat formata = new SimpleDateFormat("yyyy-MM-dd");
        String data = formata.format(dia.getTime()).toString();
        
        if (jTextFieldValor.getText().length() > 4 || Integer.parseInt(jTextFieldValor.getText()) < 0) {
            JOptionPane.showMessageDialog(null, "Valor esta incorreto");
        } else if (jTextFieldDuracao.getText().length() <= 0 || jTextFieldDuracao.getText().length() > 3) {
            JOptionPane.showMessageDialog(null, "Informe uma duração válida");
        } else if (jComboBoxFuncionarios.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Informe um funcionario.");
        } else if (jComboBoxClientes.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Informe um cliente.");
        } else if (jComboBoxHorarios.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Informe um horario.");
        } else if (SalaoApp.controle.validaHorario(jComboBoxHorarios.getSelectedItem().toString(), Integer.parseInt(
                jTextFieldDuracao.getText()), data) == false) {
            JOptionPane.showMessageDialog(null, "A duração do atendimento ultrapassa o horário disponível para este Funcionário");
        } else if (jTextFieldValor.getText().length() == 0) {
            JOptionPane.showMessageDialog(null, "Informe um valor");
        } else if (jTextAreaObs.getText().length() > 100) {
            JOptionPane.showMessageDialog(null, "Observação muito grande, favor digite um texto menor");
        } else {
            String cpf = jComboBoxFuncionarios.getSelectedItem().toString().substring(0, 14);
            SalaoApp.controle.cadastrarAtendimento(data, jComboBoxHorarios.getSelectedItem().toString(), Integer.parseInt(jTextFieldValor.getText()), jTextAreaObs.getText(), Integer.parseInt(jTextFieldDuracao.getText()), jComboBoxClientes.getSelectedItem().toString(), jComboBoxFuncionarios.getSelectedItem().toString());
            jComboBoxClientes.removeAllItems();
            jComboBoxFuncionarios.removeAllItems();
            jComboBoxHorarios.removeAllItems();
            jTextAreaObs.setText("");
            jTextFieldDuracao.setText("");
            jTextFieldValor.setText("");
        }



    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        jComboBoxClientes.removeAllItems();
        jComboBoxFuncionarios.removeAllItems();
        jComboBoxHorarios.removeAllItems();
        jTextAreaObs.setText("");
        jTextFieldDuracao.setText("");
        jTextFieldValor.setText("");
    }//GEN-LAST:event_jButton3ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private com.toedter.calendar.JCalendar jCalendar1;
    private javax.swing.JComboBox jComboBoxClientes;
    private javax.swing.JComboBox jComboBoxFuncionarios;
    private javax.swing.JComboBox jComboBoxHorarios;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextAreaObs;
    private javax.swing.JFormattedTextField jTextFieldDuracao;
    private javax.swing.JFormattedTextField jTextFieldValor;
    private java.awt.Label label1;
    // End of variables declaration//GEN-END:variables
}
